//Erstellt von Moritz Liepe
package main.editor;

import java.util.UUID;

import main.ModuleEditor;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.handlers.HandlerUtil;


public class OpenHandler extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		FileDialog fileDialog = new FileDialog(HandlerUtil.getActiveWorkbenchWindow(event).getShell(), SWT.OPEN);
		fileDialog.setFilterPath("C:/");
		String[] extensions = { "*.xml", "*.*" };
		fileDialog.setFilterExtensions(extensions);
		String fileName = fileDialog.open();
	    if (fileName != null) 
	    {
	    	VOSEditorInput vosEditorInput = new VOSEditorInput(UUID.randomUUID().toString());
			try 
			{
				HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().openEditor(vosEditorInput, VOSEditor.class.getCanonicalName());
				IWorkbenchPart part = HandlerUtil.getActivePart(event);
		    	if(part instanceof VOSEditor )
		    	{
		    		ModuleEditor mEditor = ((VOSEditor )part).getModuleEditor();
		    		mEditor.importProgram(fileName);
		    	}
			}
			catch (PartInitException e) 
			{
				// TODO Message
				e.printStackTrace();
			}
	    }
		return null;
	}

}
